package hasmap.test2;

import java.util.*;
import java.util.function.BiConsumer;

//班级80人投票选择去的景点，A,B,C,D
public class HasMapTest {
    public static void main(String[] args) {
        String[] arr = {"A","B","C","D"};
        Random random = new Random();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 80; i++) {
            int index = random.nextInt(arr.length);
//            System.out.println(arr[index]);
            list.add(arr[index]);
        }
        HashMap<String,Integer> hashMap = new HashMap<>();
        for (String name:list) {
            if(hashMap.containsKey(name)){
                //存在
                int count = hashMap.get(name);
                count++;
                hashMap.put(name,count);
            } else {
                //不存在
                hashMap.put(name,1);
            }
        }
        hashMap.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String key, Integer value) {
                System.out.println(key + "有" + value);
            }
        });
        //求最大值
        int max = 0;
        Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            int count = entry.getValue();
            if(count > max){
                max = count;
            }
        }
        //得到结果
        for (Map.Entry<String, Integer> entry : entries) {
            int count = entry.getValue();
            if(count == max){
                System.out.println(entry.getKey());
            }
        }

    }
}
